using UnityEngine;

public class CharacterStanding : MonoBehaviour
{
	public Rigidbody rigToLift;

	public AnimationCurve standForceCurve;

	public float standForceMultiplier = 1f;

	public float gravityForceMultiplayer = 1f;

	public float gravity;

	public Rigidbody duckRig;

	private Rigidbody[] rigs;

	private bool useGravity = true;

	[SerializeField]
	private ConstantForce leftKneeForce;

	[SerializeField]
	private ConstantForce rightKneeForce;

	[SerializeField]
	private CharacterInformation info;

	private void Start()
	{
		rigs = GetComponentsInChildren<Rigidbody>();
	}

	private void FixedUpdate()
	{
		if (info.freeze || Time.timeScale < 0.1f)
		{
			return;
		}
		if ((bool)leftKneeForce && (bool)rightKneeForce)
		{
			if (info.sinceGrounded < 0.3f && info.isFallen > 0f)
			{
				leftKneeForce.enabled = true;
				rightKneeForce.enabled = true;
			}
			else
			{
				leftKneeForce.enabled = false;
				rightKneeForce.enabled = false;
			}
		}
		if (info.isGrounded || !useGravity)
		{
			gravity = 0f;
		}
		else
		{
			gravity += Time.fixedDeltaTime;
		}
		if (info.isFallen > 0f)
		{
			ApplyGravity();
			if (info.isGrounded)
			{
				Stand();
			}
		}
	}

	private void ApplyGravity()
	{
		Rigidbody[] array = rigs;
		Rigidbody[] array2 = array;
		foreach (Rigidbody rigidbody in array2)
		{
			if (!(rigidbody.transform.parent.tag == "IgnoreRigidbody"))
			{
				rigidbody.AddForce(Vector3.down * Time.fixedDeltaTime * gravity * gravityForceMultiplayer, ForceMode.Acceleration);
			}
		}
	}

	private void Stand()
	{
		float num = standForceCurve.Evaluate(info.shortestDistanceFromHeadToGround) * standForceMultiplier;
		rigToLift.AddForce(Vector3.up * (num + 100f * rigToLift.velocity.magnitude) * Time.fixedDeltaTime, ForceMode.Acceleration);
	}
}
